package com.lenovo.agricultureclient.ui.dialog;

import com.lenovo.agricultureclient.ClientApp;
import com.lenovo.agricultureclient.R;
import com.lenovo.agricultureclient.bean.SensorConfig;
import com.lenovo.agricultureclient.bean.SensorValue;
import com.lenovo.agricultureclient.request.BaseRequest;
import com.lenovo.agricultureclient.request.GetConfigRequest;
import com.lenovo.agricultureclient.request.SetConfigRequest;
import com.lenovo.agricultureclient.request.BaseRequest.RequestResult;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 * 设置传感器阀值的基类
 * @author zongbingwu
 *
 */
public class SettingBaseDialog extends AppBaseDialog 
{
	//设置传感器阀值
	protected SetConfigRequest mRequest;
	//请求传感器阀值
	private GetConfigRequest mGetConfigRequest;
	//所有传感器的预警值范围
	protected SensorConfig mSensorConfig;
	//各个传感器的当前数据值
	protected SensorValue mSensorValue;
	
	protected TextView mTitleTextView;
    //传感器阀值信息
	protected LinearLayout mMajorMinLayout;
	protected TextView mMajorValueTitleTV;
	protected TextView mMajorStatusTitleTV;
	protected TextView mMajorValueTV;
	protected TextView mMajorStatusTV;
	protected EditText mMajorMinET;
	protected EditText mMajorMaxET;
	//主要用于显示空气湿度或土壤湿度阀值信息
	protected LinearLayout mSlaveLayout;
	protected TextView mSlaveValueTitleTV;
	protected TextView mSlaveStatusTitleTV;
	protected TextView mSlaveValueTV;
	protected TextView mSlaveStatusTV;
	protected EditText mSlaveMinET;
	protected EditText mSlaveMaxET;
    
    private Button mCloseBtn;
    private Button mPostBtn;
    
    private static final long DELAY_MILLIS = 1000;
    private Handler mHandler = new Handler();
    //定时器，一秒钟刷新一次界面
	private Runnable mTimeRunnable = new Runnable()
	{
		@Override
		public void run() 
		{
			onUpateView();
			mHandler.postDelayed(mTimeRunnable, DELAY_MILLIS);
		}
	};
    
    public SettingBaseDialog(Activity act, ClientApp app)
    {
    	super(act,app);
    	mSensorConfig = app.getSensorConfig();
    	mSensorValue = app.getCurSensorValue();
    }
    
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        this.setContentView(R.layout.setting_detail_dialog);
        //设置背景颜色
        getWindow().setBackgroundDrawable(new ColorDrawable(0));
        
        //初始化界面
        initView();
        //初始化子类特有的界面控件
        onInitView();
        //刷新子类特有的界面元素
        onUpateView();
        
        //设置传感器阀值
        mRequest = new SetConfigRequest();
        mRequest.setOnResponseEventListener(new BaseRequest.OnResponseEventListener() {
			@Override
			public void onResponse(BaseRequest request, RequestResult result) {
				if(mRequest.isSuccess()){
					startRequest(mGetConfigRequest);
				} else {
					dismissLoadDialog();
					showAlertDialog(getString(R.string.prompt), getString(R.string.setting_failed));
				}
			}
		});
        
        //请求传感器阀值
        mGetConfigRequest = new GetConfigRequest();
        mGetConfigRequest.setSensorConfig(mApp.getSensorConfig());
        mGetConfigRequest.setOnResponseEventListener(new BaseRequest.OnResponseEventListener() {
			@Override
			public void onResponse(BaseRequest request, RequestResult result) {
				dismissLoadDialog();
				if(mGetConfigRequest.isSuccess()){
					mApp.saveSensorConfig();
					SettingBaseDialog.this.dismiss();
				}
				SettingBaseDialog.this.dismiss();
			}
		});
        //开启定时器
        mHandler.postDelayed(mTimeRunnable, DELAY_MILLIS);
    }  
  
    //初始化界面
    private void initView()
    {
    	//关闭按钮
    	mCloseBtn = (Button)findViewById(R.id.close);
    	mCloseBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SettingBaseDialog.this.dismiss();
			}
		});
    	//对话框标题
    	mTitleTextView = (TextView)findViewById(R.id.title_text_view);
    	//传感器阀值信息
    	mMajorMinLayout = (LinearLayout)findViewById(R.id.major_min_layout);
    	mMajorValueTitleTV = (TextView)findViewById(R.id.major_left_text_view);
    	mMajorStatusTitleTV = (TextView)findViewById(R.id.major_right_text_view);
    	mMajorValueTV = (TextView)findViewById(R.id.major_value_text_view);
    	mMajorStatusTV = (TextView)findViewById(R.id.major_status_text_view);
    	mMajorMinET = (EditText)findViewById(R.id.major_min_value_edit_text);
    	mMajorMaxET = (EditText)findViewById(R.id.major_max_value_edit_text);
    	
    	mSlaveLayout = (LinearLayout)findViewById(R.id.slave_layout);
    	mSlaveValueTitleTV = (TextView)findViewById(R.id.slave_left_text_view);
    	mSlaveStatusTitleTV = (TextView)findViewById(R.id.slave_right_text_view);
    	mSlaveValueTV = (TextView)findViewById(R.id.slave_value_text_view);
    	mSlaveStatusTV = (TextView)findViewById(R.id.slave_status_text_view);
    	mSlaveMinET = (EditText)findViewById(R.id.slave_min_value_edit_text);
    	mSlaveMaxET = (EditText)findViewById(R.id.slave_max_value_edit_text);
    	
    	//确定按钮
    	mPostBtn = (Button)findViewById(R.id.ok_button);
    	mPostBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				settingEnter();
			}
		});
    }
    //初始化子类特有的界面控件
    protected void onInitView() {}
    //刷新子类特有的界面元素
    protected void onUpateView() {}
    //点击“确定”按钮以后，触发子类进行设置操作
    protected boolean onPostSet(int majorMin, int majorMax, int slaveMin, int slaveMax) 
    { 
    	return false;
    }
    //点击“确定”按钮以后触发设置操作
    private void settingEnter()
    {
    	int majorMin = 0;
    	int majorMax = 0;
    	int slaveMin = 0;
    	int slaveMax = 0;
    	boolean isCan = true;
    	//提取数据
    	String majorMinStr = mMajorMinET.getText().toString().trim();
    	String majorMaxStr = mMajorMaxET.getText().toString().trim();
    	String slaveMinStr = mSlaveMinET.getText().toString().trim();
    	String slaveMaxStr = mSlaveMaxET.getText().toString().trim();
    	//数据检查
    	if(majorMaxStr.equals("")){
    		showAlertDialog(getString(R.string.prompt), getString(R.string.min_max_value_not_empty));
    		return;
    	}
    	try {
    		majorMax = Integer.parseInt(majorMaxStr);
		} catch (Exception e) {
			e.printStackTrace();
			isCan = false;
		}
    	
    	//根据界面的显示情况，提取有用的值
    	if(mMajorMinLayout.getVisibility()==View.VISIBLE)
    	{
    		if(majorMinStr.equals("")){
        		showAlertDialog(getString(R.string.prompt), getString(R.string.min_max_value_not_empty));
        		return;
        	}
    		try {
        		majorMin = Integer.parseInt(majorMinStr);
    		} catch (Exception e) {
    			e.printStackTrace();
    			isCan = false;
    		}
    	}
    	//根据界面的显示情况，提取有用的值
    	if(mSlaveLayout.getVisibility()==View.VISIBLE)
    	{
    		if(slaveMaxStr.equals("")){
        		showAlertDialog(getString(R.string.prompt), getString(R.string.min_max_value_not_empty));
        		return;
        	}
        	try {
        		slaveMax = Integer.parseInt(slaveMaxStr);
    		} catch (Exception e) {
    			e.printStackTrace();
    			isCan = false;
    		}
        	if(slaveMinStr.equals("")){
        		showAlertDialog(getString(R.string.prompt), getString(R.string.min_max_value_not_empty));
        		return;
        	}
    		try {
    			slaveMin = Integer.parseInt(slaveMinStr);
    		} catch (Exception e) {
    			e.printStackTrace();
    			isCan = false;
    		}
    	}
    	//具体的设置请求，由子类来实现
    	if(isCan && onPostSet(majorMin,majorMax,slaveMin, slaveMax)){
    		showLoadDialog();
    	}
    }
}
